<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_LogController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        $this->loadTemplate('alpona');
        $this->view->headTitle = "Mua bán hàng ngày";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js', 'jquery-ui-1.10.3.custom.min.js', 'autoNumeric.js');
		parent::addScripts($scripts);
        parent::addStyleSheet(array('jquery-ui-1.10.3.custom.css'));
        
        $request = $this->_request;
        
        $currYear = date("Y");
        
        $currentDateSub1Week = date("d/m/Y", strtotime("01/01/$currYear"));
        $currentDate = date("d/m/Y");
        
        $dateFrom = $request->getParam('from', false);
        $dateTo = $request->getParam('to', false);
        
        if(!$dateFrom){
            $dateFrom = $currentDateSub1Week;
        }else{
            $dateFrom = urldecode($dateFrom);
        }
        
        if(!$dateTo){
            $dateTo = $currentDate;
        }else{
            $dateTo = urldecode($dateTo);
        }
        
        $customer_model = new Admin_Model_Customer();
        $customers = $customer_model->getDatas();
        
        $supplier_model = new Admin_Model_Supplier();
        $suppliers = $supplier_model->getDatas();
        
        $this->view->customers = $customers;
        $this->view->suppliers = $suppliers;
        
        $model = new Admin_Model_Log();
        $data = $model->getDatas($dateFrom, $dateTo);
        $totalBuy = $model->getTotalBuy();
        $totalSale = $model->getTotalSale();
        $totalPayment = $model->getTotalPayment();
        $totalOrderBuys = $model->getTotalOrderBuys();
        
        $this->view->data = $data;
        $this->view->dateFrom = $dateFrom;
        $this->view->dateTo = $dateTo;
        $this->view->totalBuy = current($totalBuy);
        $this->view->totalSale = current($totalSale);
        $this->view->totalPayment = current($totalPayment);
        $this->view->totalOrderBuys = current($totalOrderBuys);
	}
    
    public function addAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $data = $this->parseParams($request);
            
            $model = new Admin_Model_Log();
            $result = $model->saveData($data);
            
            echo $result;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function addPaymentAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $data = $this->parseParams($request);
            
            $model = new Admin_Model_Log();
            $result = $model->saveData($data);
            
            echo $result;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    public function deleteAction(){
        $request = $this->_request;
        
        if($request->isXmlHttpRequest()){
            $id = $request->getParam('id', false);
            
            if($id != false){
                $model = new Admin_Model_Log();
                $result = $model->deleteData($id);
                
                echo $result;
            }
            
            echo false;
        }
        
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();
    }
    
    private function parseParams($request){
        $result = array();
        
        $result['name'] = $request->getParam('name');
        $result['total'] = $request->getParam('total');
        $result['type'] = $request->getParam('type');
        
        if($result['type'] == 2){
            $result['receiver'] = $request->getParam('receiver');
        }else{
            $result['receiver'] = null;
        }
        
        $date = $request->getParam('date');
        $date = DateTime::createFromFormat('d-m-Y', $date);
        
        $result['date'] = $date->format("Y-m-d");
        
        return $result;
    }
}